import Mock from "mockjs"
import {
    param2Obj
} from "../utils/myUtils"
let tableData = []

for (let i = 1; i <= 100; i++) {
    let num = Mock.mock('@integer(0, 2)')
    tableData.push({
        ID: Mock.mock('@guid'),
        id: 1,
        name: ["老年人基础套餐A", "儿童全身基础套餐B", "基础疾病检查套餐C"][num],
        image: num,
        type: ["老年人套餐", "儿童套餐", "通用套餐"][num],
        region: ["东院区", "西院区", "北院区"][num],
        price: [1200, 600, 800][num],
        project: ["一般检查,内科检查,外科检查,血常规,尿常规", "生长及营养评价,大运动发育检查,言语筛查,过敏原检查,特殊专科筛查", "血生化检查,包括血液常规,肝功能,肾功能,乳腺彩色多普勒超声检查(女),妇科检查"][num],
        state: Mock.mock('@boolean')
    })
}
export default {

    getTableData: (config) => {
        let keyWord = Number(param2Obj(config.url).keyWord);
        let page = Number(param2Obj(config.url).page)
        let pageSize = Number(param2Obj(config.url).pageSize)

        console.log(keyWord, page, pageSize);
        let res = tableData.filter(item => {
            return item.type == ["", "老年人套餐", "儿童套餐", "通用套餐"][keyWord]
        })
        console.log(res, keyWord);
        let arr = keyWord == 0 ? tableData : res
        let Arr = arr.filter((item, index) => {
            return index >= (page - 1) * pageSize && index < page * pageSize
        })

        return {
            code: 200,
            msg: "获取数据成功",
            data: Arr,
            title: arr.length
        }
    },
    delData: (config) => {
        let id = JSON.parse(config.body).ID
            // console.log(id);
        tableData.forEach((item, index) => {
            if (item.ID == id) {
                tableData.splice(index, 1)

                // console.log(tableData);
            }
        })
        return {
            code: 200,
            msg: "获删除据成功",
            data: null,

        }
    },
    batchDel: (config) => {
        let {
            ID
        } = JSON.parse(config.body);
        let res = tableData.filter(item => {
            return ID.indexOf(item.ID) == -1
        })
        tableData = res
        return {
            code: 200,
            msg: "批量删除成功",
            data: null
        }
    },
    addData: (config) => {
        let data = JSON.parse(config.body)
        tableData.unshift({
            ID: new Date().getTime(),
            id: 1,
            name: data.name,
            type: ["老年人套餐", "儿童套餐", "通用套餐"][data.type],
            price: data.price,
            project: data.project,
            state: data.state,
            region: ["东院区", "西院区", "北院区"][data.region]
        })
        console.log(tableData);
        console.log(data);
        return {
            code: 200,
            msg: "添加数据成功",
            data: null
        }
    },
    modifyData: (config) => {
        let {
            ID,
            name,
            price,
            project,
            type,
            region,
            state
        } = JSON.parse(config.body)
        tableData.forEach(item => {
            if (item.ID == ID) {
                item.name = name;
                item.type = type;
                item.region = region;
                item.type = type;
                item.project = project;
                item.state = state;
                item.price = price;
            }
        })
        return {
            code: 200,
            msg: "修改数据成功",
            data: null
        }
    }

}